home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club (Business) 1997 July / Software of the Month Club - Business (Volume 239) (July 1997).iso / pc / code / shared.dxr / 04478_Sound Engine.ls < prev    next >
Encoding:
Text File  |  1996-02-21  |  13.0 KB  |  395 lines

  1. global gSoundChannels, gSoundPath, gsoundLevel, gKeyFrameSound, gKeyFrameSoundMode, gEnvironmentSound, gLastSoundChannel, gCurKeyList, sQTCharSprite, sQTSPRITE
  2.  
  3. on setAmbientSound
  4.   set snd to getaProp(gCurKeyList, #Audio)
  5.   set gKeyFrameSound to getaProp(snd, #enter)
  6.   set gKeyFrameSoundMode to getaProp(snd, #exit)
  7.   set theEnvironmentSound to getaProp(snd, #loop)
  8.   if theEnvironmentSound = #NULL then
  9.     stopSound(2)
  10.   else
  11.     if theEnvironmentSound <> #noLoop then
  12.       playSound(string(theEnvironmentSound), #ambientLoop)
  13.     else
  14.       nothing()
  15.     end if
  16.   end if
  17.   if gKeyFrameSound <> #noEnter then
  18.     playSound(string(gKeyFrameSound), #keyframeFile)
  19.     if gKeyFrameSoundMode = #KFSOUND then
  20.       alert("bad sound definition. gKeyFrameSoundMode = #KFSOUND")
  21.       playSound(string(gKeyFrameSound), #keyframeFile)
  22.     else
  23.       if gKeyFrameSoundMode = #LOOPEDKFSOUND then
  24.         alert("bad sound definition. gKeyFrameSoundMode = #LOOPEDKFSOUND")
  25.         playSound(string(gKeyFrameSound), #keyframeFileLooped)
  26.       end if
  27.     end if
  28.   end if
  29. end
  30.  
  31. on serviceSound
  32.   repeat with Channel = 1 to 8
  33.     set theSoundData to getaProp(gSoundChannels, Channel)
  34.     if not voidp(theSoundData) then
  35.       if the status of theSoundData = #paused then
  36.         next repeat
  37.       end if
  38.       if getOne([#ambientLoop, #keyframeFileLooped, #FileLooped, #animatorFile], the type of theSoundData) > 0 then
  39.         if not soundBusy(Channel) then
  40.           sound playFile Channel, gSoundPath & the name of theSoundData
  41.         end if
  42.         next repeat
  43.       end if
  44.       if (the status of theSoundData = #playing) and not soundBusy(Channel) then
  45.         deleteProp(gSoundChannels, Channel)
  46.       end if
  47.     end if
  48.   end repeat
  49. end
  50.  
  51. on setSoundVolumes
  52.   set the soundLevel to gsoundLevel
  53.   set relativeVolume to gsoundLevel * 36
  54.   set the volume of sprite sQTCharSprite to relativeVolume
  55.   set the volume of sprite sQTSPRITE to relativeVolume
  56.   repeat with Channel in [2, 3, 4, 5, 6, 7, 8]
  57.     set the volume of sound Channel to relativeVolume
  58.   end repeat
  59.   set theSoundData to getaProp(gSoundChannels, 1)
  60.   if not voidp(theSoundData) then
  61.     set the volume of sound 1 to relativeVolume
  62.   end if
  63. end
  64.  
  65. on stopSound nameOrChannelToStop
  66.   if nameOrChannelToStop = #ALL then
  67.     puppetSound(0)
  68.     repeat with Channel = 1 to 8
  69.       sound stop Channel
  70.     end repeat
  71.     exit
  72.   else
  73.     if integerp(nameOrChannelToStop) then
  74.       if nameOrChannelToStop = 1 then
  75.         puppetSound(0)
  76.       end if
  77.       sound stop nameOrChannelToStop
  78.       deleteProp(gSoundChannels, nameOrChannelToStop)
  79.       exit
  80.     else
  81.       repeat with Channel = 1 to 8
  82.         set theSoundData to getaProp(gSoundChannels, Channel)
  83.         if not voidp(theSoundData) then
  84.           set name to the name of theSoundData
  85.           if name = nameOrChannelToStop then
  86.             if Channel = 1 then
  87.               puppetSound(0)
  88.             end if
  89.             sound stop Channel
  90.             deleteProp(gSoundChannels, Channel)
  91.             exit
  92.           end if
  93.         end if
  94.       end repeat
  95.     end if
  96.   end if
  97. end
  98.  
  99. on stopNonAmbientSounds
  100.   repeat with Channel in [1, 3, 4, 5, 6, 7, 8]
  101.     set theSoundData to getaProp(gSoundChannels, Channel)
  102.     if not voidp(theSoundData) then
  103.       if getOne([#keyframeCast, #keyframeFile, #keyframeFileLooped, #animatorCast, #animatorFile], the type of theSoundData) > 0 then
  104.         deleteProp(gSoundChannels, Channel)
  105.         sound stop Channel
  106.       end if
  107.     end if
  108.   end repeat
  109. end
  110.  
  111. on SelectPlayChannel type, name
  112.   if type = #ambientLoop then
  113.     set Channel to 2
  114.     return Channel
  115.   else
  116.     set freeChannel to 0
  117.     repeat with Channel = 8 down to 4
  118.       set theSoundData to getaProp(gSoundChannels, Channel)
  119.       if voidp(theSoundData) then
  120.         set freeChannel to Channel
  121.         next repeat
  122.         next repeat
  123.       end if
  124.       if getaProp(theSoundData, #name) = name then
  125.         set freeChannel to Channel
  126.         exit repeat
  127.         next repeat
  128.       end if
  129.       if not soundBusy(Channel) and (not getOne([#ambientLoop, #FileLooped, #animatorCast, #animatorFile, #animatorFileLooped], type) > 0) then
  130.         set freeChannel to Channel
  131.         next repeat
  132.       end if
  133.     end repeat
  134.     return freeChannel
  135.   end if
  136. end
  137.  
  138. on CheckChannelStatus Channel, name
  139.   set theSoundData to getaProp(gSoundChannels, Channel)
  140.   if not voidp(theSoundData) then
  141.     if (getaProp(theSoundData, #name) = name) and (getaProp(theSoundData, #status) = #playing) then
  142.       return #playing
  143.     end if
  144.   else
  145.     return #true
  146.   end if
  147. end
  148.  
  149. on playSound name, type, bWaitFor
  150.   set Start to the timer
  151.   set Channel to SelectPlayChannel(type, name)
  152.   if Channel <> 0 then
  153.     set status to CheckChannelStatus(Channel, name)
  154.     if status = #playing then
  155.       set gLastSoundChannel to Channel
  156.       return Channel
  157.     end if
  158.     setaProp(gSoundChannels, Channel, [#name: name, #type: type, #status: #playing])
  159.     if getOne([#cast, #animatorCast, #keyframeCast], type) > 0 then
  160.       puppetSound(Channel, cast the number of cast name)
  161.     else
  162.       if getOne([#ambientLoop, #File, #FileLooped, #animatorFile, #keyframeFile, #keyframeFileLooped], type) > 0 then
  163.         sound playFile Channel, gSoundPath & name
  164.       else
  165.         displayChaosError(#info, "Sound type undefined:" && string(type))
  166.         exit
  167.       end if
  168.     end if
  169.   else
  170.     displayChaosError(#warning, "Ran out of sound channels attempting to play:" && name)
  171.     exit
  172.   end if
  173.   if (bWaitFor = #NOW) and (Channel <> 0) then
  174.     updateStage()
  175.   else
  176.     if bWaitFor and (Channel <> 0) then
  177.       cursor([the number of cast "NullCursor", the number of cast "NullCursor.mask"])
  178.       updateStage()
  179.       repeat while soundBusy(Channel)
  180.         cursorandupdate()
  181.       end repeat
  182.     end if
  183.   end if
  184.   set gLastSoundChannel to Channel
  185.   return Channel
  186. end
  187.  
  188. on waitForSoundDone Channel
  189.   repeat while soundBusy(Channel)
  190.     cursorandupdate()
  191.   end repeat
  192. end
  193.  
  194. on setSoundState action
  195.   if action = #pause then
  196.     puppetSound(0)
  197.     repeat with Channel = 1 to 8
  198.       set theSoundData to getaProp(gSoundChannels, Channel)
  199.       if not voidp(theSoundData) then
  200.         set the status of getaProp(gSoundChannels, Channel) to #paused
  201.         sound stop Channel
  202.       end if
  203.     end repeat
  204.     exit
  205.   end if
  206.   if action = #play then
  207.     repeat with Channel = 1 to 8
  208.       set theSoundData to getaProp(gSoundChannels, Channel)
  209.       if not voidp(theSoundData) then
  210.         if the type of theSoundData = #keyframeCast then
  211.           deleteProp(gSoundChannels, Channel)
  212.           next repeat
  213.         end if
  214.         playSound(the name of theSoundData, the type of theSoundData)
  215.       end if
  216.     end repeat
  217.     exit
  218.   end if
  219.   displayChaosError(#info, "Sound state undefined:" && string(action))
  220. end
  221.  
  222. on displayChaosError type, message
  223.   if type = #info then
  224.     set errorMessage to "Chaos Error - Information" & RETURN & RETURN & message
  225.   else
  226.     if type = #warning then
  227.       set errorMessage to "Chaos Error - Warning" & RETURN & RETURN & message
  228.     else
  229.       if type = #severe then
  230.         set errorMessage to "Chaos Error - Severe" & RETURN & RETURN & message & RETURN & RETURN & "You should probably quit now."
  231.       end if
  232.     end if
  233.   end if
  234.   alert(errorMessage)
  235. end
  236.  
  237. on playSoundTerry name, type, bWaitFor
  238.   if type = #ambientCast then
  239.     set Channel to 1
  240.     puppetSound(name)
  241.     set theSoundData to [:]
  242.     addProp(theSoundData, #name, name)
  243.     addProp(theSoundData, #type, type)
  244.     addProp(theSoundData, #status, #playing)
  245.     setaProp(gSoundChannels, Channel, theSoundData)
  246.     if bWaitFor then
  247.       repeat while soundBusy(Channel)
  248.         cursorandupdate()
  249.       end repeat
  250.     end if
  251.     set gLastSoundChannel to Channel
  252.     return Channel
  253.   else
  254.     if type = #ambientLoop then
  255.       set Channel to 2
  256.       set theSoundData to getaProp(gSoundChannels, Channel)
  257.       if voidp(theSoundData) then
  258.         set theSoundData to [:]
  259.         addProp(theSoundData, #name, name)
  260.         addProp(theSoundData, #type, type)
  261.         addProp(theSoundData, #status, #playing)
  262.         setaProp(gSoundChannels, Channel, theSoundData)
  263.       else
  264.         if the name of theSoundData = name then
  265.           if the status of theSoundData = #playing then
  266.             if bWaitFor then
  267.               waitForSoundDone(Channel)
  268.             end if
  269.             set gLastSoundChannel to Channel
  270.             return Channel
  271.           else
  272.             set the status of getaProp(gSoundChannels, Channel) to #playing
  273.           end if
  274.         else
  275.           set the name of getaProp(gSoundChannels, Channel) to name
  276.           set the type of getaProp(gSoundChannels, Channel) to type
  277.           set the status of getaProp(gSoundChannels, Channel) to #playing
  278.         end if
  279.       end if
  280.       sound playFile Channel, gSoundPath & name
  281.       if bWaitFor then
  282.         waitForSoundDone(Channel)
  283.       end if
  284.       set gLastSoundChannel to Channel
  285.       return Channel
  286.     else
  287.       if (type = #keyframeFile) or (type = #keyframeFileLooped) or (type = #keyframeCast) then
  288.         set Channel to 3
  289.         set theSoundData to getaProp(gSoundChannels, Channel)
  290.         if voidp(theSoundData) then
  291.           set theSoundData to [:]
  292.           addProp(theSoundData, #name, name)
  293.           addProp(theSoundData, #type, type)
  294.           addProp(theSoundData, #status, #playing)
  295.           addProp(gSoundChannels, Channel, theSoundData)
  296.         else
  297.           if the name of theSoundData = name then
  298.             if the status of theSoundData = #playing then
  299.               if bWaitFor then
  300.                 waitForSoundDone(Channel)
  301.               end if
  302.               set gLastSoundChannel to Channel
  303.               return Channel
  304.             else
  305.               set the status of getaProp(gSoundChannels, Channel) to #playing
  306.             end if
  307.           else
  308.             set the name of getaProp(gSoundChannels, Channel) to name
  309.             set the type of getaProp(gSoundChannels, Channel) to type
  310.             set the status of getaProp(gSoundChannels, Channel) to #playing
  311.           end if
  312.         end if
  313.         if type = #keyframeCast then
  314.           puppetSound(Channel, cast the number of cast name)
  315.         else
  316.           sound playFile Channel, gSoundPath & name
  317.         end if
  318.         if bWaitFor then
  319.           waitForSoundDone(Channel)
  320.         end if
  321.         set gLastSoundChannel to Channel
  322.         return Channel
  323.       else
  324.         if getOne([#File, #FileLooped, #cast, #animatorCast, #ambientLoop, #animatorFileLooped], type) > 0 then
  325.           set freeChannel to 0
  326.           repeat with Channel = 4 to 8
  327.             set theSoundData to getaProp(gSoundChannels, Channel)
  328.             if not voidp(theSoundData) then
  329.               if (the name of theSoundData = name) and (the type of theSoundData = type) then
  330.                 if the status of theSoundData = #playing then
  331.                   if bWaitFor then
  332.                     waitForSoundDone(Channel)
  333.                   end if
  334.                   set gLastSoundChannel to Channel
  335.                   return Channel
  336.                 else
  337.                   set the status of getaProp(gSoundChannels, Channel) to #playing
  338.                   if bWaitFor then
  339.                     waitForSoundDone(Channel)
  340.                   end if
  341.                   set gLastSoundChannel to Channel
  342.                   return Channel
  343.                 end if
  344.               else
  345.                 if (the status of theSoundData = #playing) and not soundBusy(Channel) and (the type of theSoundData <> #FileLooped) and (the type of theSoundData <> #animatorFileLooped) then
  346.                   set freeChannel to Channel
  347.                   exit repeat
  348.                 end if
  349.               end if
  350.               next repeat
  351.             end if
  352.             set freeChannel to Channel
  353.             exit repeat
  354.           end repeat
  355.           if freeChannel > 0 then
  356.             set theSoundData to getaProp(gSoundChannels, freeChannel)
  357.             if voidp(theSoundData) then
  358.               set theSoundData to [:]
  359.               addProp(theSoundData, #name, name)
  360.               addProp(theSoundData, #type, type)
  361.               addProp(theSoundData, #status, #playing)
  362.               addProp(gSoundChannels, freeChannel, theSoundData)
  363.             else
  364.               set the name of getaProp(gSoundChannels, freeChannel) to name
  365.               set the type of getaProp(gSoundChannels, freeChannel) to type
  366.               set the status of getaProp(gSoundChannels, freeChannel) to #playing
  367.             end if
  368.           else
  369.             displayChaosError(#warning, "Ran out of sound channels attempting to play:" && name)
  370.             set gLastSoundChannel to Channel
  371.             return 0
  372.           end if
  373.           if getOne([#cast, #animatorCast, #keyframeCast], type) > 0 then
  374.             puppetSound(freeChannel, cast the number of cast name)
  375.           else
  376.             sound playFile freeChannel, gSoundPath & name
  377.           end if
  378.           if bWaitFor then
  379.             waitForSoundDone(Channel)
  380.           end if
  381.           set gLastSoundChannel to Channel
  382.           return freeChannel
  383.         else
  384.           displayChaosError(#info, "Sound type undefined:" && string(type))
  385.           set gLastSoundChannel to Channel
  386.           return 0
  387.         end if
  388.       end if
  389.     end if
  390.   end if
  391. end
  392.  
  393. on checkAmbientsound
  394. end
  395.